Enhanced for Loop

1. Enhanced for Loop 개요

컬렉션 및 배열에서 간단한 구문을 제공한다.
반복문에 반복할 길이와 증가치를 정의할 필요가 없다.

2. 선언방법


for (String str:str_arr){
         System.out.println(str);
}

3. 예제

- 기존코드


public class Before_Loop{
	public static void main(String[] args){
		String[] str_arr = {"one","two","three","four"};
		
		for (int i=0; i <str_arr.length; i++){
			System.out.println(str_arr[i]);
		}
	}
}

- JAVA 5.0 코드


public class After_Loop{
	public static void main(String[] args){
		String[] str_arr = {"one","two","three","four"};

		for (String str:str_arr){
			System.out.println(str);
		}
	}
}